package com.xywy.tangandroid;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.activity.FrameAct;
import com.xywy.tangandroid.activity.IntroducAct;
import com.xywy.tangandroid.activity.LoginAct;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.util.SaveLoginPreference;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.util.UserKeeper;
import com.xywy.tangandroid.vollery.GetRequest;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

public class MainActivity extends BaseActivity {


	private ImageView spash;
	private AlphaAnimation aa;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_main);
		
		aa = new AlphaAnimation(0.1f, 1.0f);
		aa.setDuration(1000);
		spash = (ImageView) findViewById(R.id.splash_img);
		spash.setAnimation(aa);
	}

	@Override
	protected void initListener() {
		aa.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				if("1".equals(Constant.getFirstLogin(context))){
					

					
					String[] loginInfo=SaveLoginPreference.getParam(context);	
					String username=loginInfo[0];
					String password=loginInfo[1];
					System.out.println("username.."+username+"....password.."+password);
					if(username!=null&&password!=null){
						//发送请求 
						login(username, password);
					}else{
						Intent intent=new Intent(MainActivity.this, LoginAct.class);
						startActivity(intent);
						MainActivity.this.finish();
					 }	
					
				}else{
				Intent intent=new Intent(MainActivity.this, IntroducAct.class);
				startActivity(intent);
				MainActivity.this.finish();}
			}
		});
	}

	@Override
	protected void initData() {
		
	}

	@Override
	public void onClick(View v) {
		
	}
	// 登录
		private void login(final String  username ,final String password) {
			PostRequest<String> postRequest = new PostRequest<String>(Constant.getUrl(
					"kbb", "users") + "&type=login", String.class,
					new OnVolleyResponseListener<String>() {
				@Override
				public void onSuccess(String response) {
					UIUtil.DissDialog(context, waittingDialog);
					
					if (response != null) {
						JSONObject object;
						try {
							object = new JSONObject(response);
							int state = object.optInt("state");
							String error = object.optString("error");
							if (state == 200) {
								User.getIUserInstance(context).setUserid(
										object.optString("userid"));
								User.getIUserInstance(context).setBirthday(
										object.optString("birthday"));
								User.getIUserInstance(context).setSex(
										object.optString("sex"));
								User.getIUserInstance(context).setHeight(
										object.optInt("height"));
								User.getIUserInstance(context).setPhonenum(
										object.optString("phonenum"));
								User.getIUserInstance(context).setIsregister(
										object.optString("isregister"));

								System.out.println("打印一下获取的userid"
										+ User.getIUserInstance(context)
												.getUserid());
								
								SaveLoginPreference.saveParam(context, username, password);

								getUserNameFromServer(User.getIUserInstance(
										context).getUserid());
							} else {
								UIUtil.showToast(context, error);
								Intent intent = new Intent(context,
										LoginAct.class);
								startActivity(intent);
								finish();
								
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

				}}

				@Override
				public void onError(VolleyError volleyError) {
					UIUtil.DissDialog(context, waittingDialog);
					UIUtil.showToast(context, context.getResources()
							.getString(R.string.network_slow));
					Intent intent = new Intent(context,
							LoginAct.class);
					startActivity(intent);
					finish();
				}

			});
			HashMap<String, String> params = new HashMap<String, String>();
			params.put("phonenum", username);// phonenum
			params.put("password", password);// password
			postRequest.setParams(params);
			postRequest.setIsParseJson(false);
			executeRequest(postRequest);

		}
		/**
		 * 获取用户名
		 */
		private void getUserNameFromServer(String userid) {
			try {
				String url = Constant.url
						+ "act=kbb&fun=users&type=pullAccountInfo&tag=wjk&userid="
						+ userid + "&sign="
						+ MD5.md5s(Constant.Sign + Constant.wjkTag);
				Log.e("getnameUrl ", url);
				GetRequest<String> getNameRequest = new GetRequest<String>(url,
						String.class, new OnVolleyResponseListener<String>() {

							@Override
							public void onError(VolleyError volleyError) {
								// TODO Auto-generated method stub
								System.out.println(volleyError.getCause());
								Log.e("getuserName-error", volleyError
										.getStackTrace().toString());
								Intent intent = new Intent(context,
										LoginAct.class);
								startActivity(intent);
								finish();
							}

							@Override
							public void onSuccess(String response) {
								
								System.out.println("获取名字的问题。。"+response);
								try {
									JSONObject json = new JSONObject(response);
									int state = json.optInt("state");
									if (state == 200) {
										JSONArray array = json
												.optJSONArray("accounts");
										for (int i = 0; i < array.length(); i++) {
											JSONObject item = (JSONObject) array
													.get(i);
											int accountid = item
													.optInt("accountid");
											if (accountid == 0) {
												User.getIUserInstance(context)
														.setAccountstr(
																item.optString("accountstr"));
												UserKeeper.saveUserInfo(
														User.getIUserInstance(context),
														MainActivity.this);
												break;
											}
										}
									}
//									if(state == 200){
									Intent intent = new Intent(context,
											FrameAct.class);
									startActivity(intent);
									finish();
									
//									}else{
//										UIUtil.showToast(context, "登录 失败");
//										Intent intent = new Intent(context,
//												LoginAct.class);
//										startActivity(intent);
//										finish();
//									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
				getNameRequest.setIsParseJson(false);
				executeRequest(getNameRequest);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
}
